package com.ys.web.user.info;

import cn.hutool.core.util.IdcardUtil;
import com.ys.base.BaseConstants;
import com.ys.base.BaseParam;
import com.ys.base.EnumType;
import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.*;
import com.ys.utils.datasource.DynamicDataSourceContextHolder;
import com.ys.utils.http.RSAUtils;
import com.ys.utils.pay.PasswordHelper;
import com.ys.utils.redis.Redis;
import com.ys.utils.redis.key.UserKey;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import com.ys.web.user.login.LoginDao;
import com.ys.web.user.login.LoginInfo;
import com.ys.web.user.login.LoginService;
import com.ys.web.yinsheng.model.RequestData;
import com.ys.web.yinsheng.model.UserMerchant;
import com.ys.web.yinsheng.service.YinShengService;
import com.ys.web.yinsheng.util.ServiceEnum;
import com.ys.web.yinsheng.util.YinShengClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/user/info/")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private LoginService loginService;

    @Autowired
    private LoginDao loginDao;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private YinShengService yinShengService;

    /**
     * 修改用户基础信息
     *
     * @param editValue 修改后的值
     * @param type      修改的值类型 1昵称 2头像 3电话
     * @return
     */
    @RequestMapping("edit/base")
    public String editNickName(@RequestAttribute String uid, String editValue, int type) {
        UserInfo info = new UserInfo();
        LoginInfo loginInfo = new LoginInfo();
        info.setId(uid);
        int i;
        if (type == 1) {
            String key = String.format("ys:user:info:nickName:count:%s:", uid);
            //判断间隔数据是否存在
            if(Redis.key.exists(key)){
                SystemError.wrapBs("400","修改失败，修改昵称每三天一次");
            }
            String newStr = editValue.trim().replace(" ", "");
            String keyWord = Config.pp.get("msg.data.key.word.list");
            newStr = StringHandler.keywordReplace(keyWord,newStr);
            info.setNickName(newStr);
            loginInfo.setNickName(newStr);
            //判断用户名是否存在
            int count  = userInfoService.getNickNameInfo(newStr);
            if(count>0){
                SystemError.wrapBs("400","修改失败，修改昵称已存在");
            }
            i = userInfoService.updateUserNickName(info);
            if(i>0){
                //添加间隔数据
                Redis.str.set(key,"1",StringHandler.getInt(Config.pp.get("ys:user:info:nickName.update.time"),3)*24*60*60);
            }
        } else if (type == 2) {
            info.setAvatarUrl(editValue);
            loginInfo.setAvatarUrl(editValue);
            i = userInfoService.updateUserAvatarUrl(info);
        } else if (type == 3) {
            LoginInfo loginInfo1 = loginService.infoByUid(uid);
            Redis.key.del("user:phone:"+loginInfo1.phone);
            info.setPhone(editValue);
            loginInfo.setPhone(editValue);

            i = userInfoService.updateUserPhone(info);
        } else {
            return ResponseHandler.success();
        }
        if (i == 1) {
            String userKey = UserKey.userInfoByUid(uid);
            Redis.hash.hmset(userKey, MapHandler.obj2StrNotNull(JsonUtil.beanToMap(loginInfo)), LoginService.loginInfoTimeOut);
            return ResponseHandler.success();
        } else {
            return ResponseHandler.error(EnumType.ResponseCode.Fail);
        }
    }

    /**
     * 设置支付密码
     *
     * @param uid
     * @param sms
     * @param pass
     * @return
     */
    @RequestMapping("setPayPass")
    public String setPayPass(@RequestAttribute String uid, String sms, String pass) {
        LoginInfo loginInfo = loginService.infoByUid(uid);
        String whiteListStr = Config.pp.getOrDefault(BaseConstants.DataSourceConstants.PAY_PASSWORD_WHITE_LIST, "18290423943");
        List<String> list = Arrays.stream(whiteListStr.split(",")).collect(Collectors.toList());
        //如果不在白名单内
        if (!list.contains(loginInfo.getPhone())) {
            String sms2 = Redis.str.get(UserKey.codeByPhone(loginInfo.getPhone()));
            if (StringHandler.isEmpty(sms2)) {
                SystemError.wrapBs("500", "请先获取验证码");
            }
            if (!sms2.equals(sms)) {
                SystemError.wrapBs("500", "验证码已过期");
            }
        }
        loginInfo.setPayPass(PasswordHelper.encryptPassword(RSAUtils.decryptField(pass), loginInfo.getSalt()));
        int i = loginService.updatePayPassWord(loginInfo);
        if (i != 1) {
            SystemError.wrapBs("500", "设置失败");
        }
        //验证通过
        Redis.key.del(UserKey.userInfoByUid(uid));
        return ResponseHandler.success();
    }

    /**
     * 开户
     *
     * @param uid
     * @param realName
     * @param realIdCard
     * @return
     */
    @RequestMapping("real")
    public String real(@RequestAttribute String uid, String realName, String realIdCard,
                        @RequestParam(defaultValue = "") String phone,String token) {
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            SystemError.wrapBs("500", "系统维护,交易系统已关闭");
        }
        LoginInfo loginInfo = loginService.infoByUid(uid);
        String phoneKey = "";
        if(!phone.equals(loginInfo.phone)){
            phoneKey = UserKey.uidByPhone(loginInfo.phone);
        }
        if (StringHandler.isEmpty(loginInfo.getRealIdCard())) {
            if (StringHandler.isEmpty(realIdCard)) {
                SystemError.wrapBs("500", "身份证号码不能为空");
            }
            if (!IdcardUtil.isValidCard(realIdCard)) {
                return ResponseHandler.error("500", "身份证号码格式错误");
            }
            int age = IdcardUtil.getAgeByIdCard(realIdCard);
            if (age < 18 || age > 65) {
                SystemError.wrapBs("500", "为防止银行卡盗刷，本平台仅支持18-65岁以内用户使用");
            }
            if(StringHandler.isEmpty(phone) && StringHandler.isEmpty(loginInfo.getPhone())){
                SystemError.wrapBs("500", "请填写电话号码");
            }
            Integer id = userInfoService.getOpenAccountCountByPhone(phone);
            if (StringHandler.isNotEmpty(id)) {
                if(id!=StringHandler.getInt(uid)){
                    SystemError.wrapBs("500", "该电话号码认证，不可重复操作");
                }
            }
            if(!phone.equals(loginInfo.getPhone())){
                loginInfo.setPhone(phone);
            }
            id = userInfoService.getOpenAccountCountByCardNo(realIdCard);
            if (StringHandler.isNotEmpty(id)) {
                if(id==StringHandler.getInt(uid)){
                    Redis.key.del(UserKey.userInfoByUid(uid));
                    return ResponseHandler.success();
                }
                SystemError.wrapBs("500", "该身份证已认证，不可重复操作");
            }
            if (realName.length() > StringHandler.getInt(Config.pp.getOrDefault("wallet.create.name.len", "15"))) {
                SystemError.wrapBs("500", "请输入身份证上的真实姓名");
            }
            String key = String.format("ys:user:info:real:count:%s:", uid);
            long countRel = Redis.key.increment(key);
            Redis.key.setKeyTimeout(key,30*24*60*60L);
            if (countRel>0) {
                String setCount = Config.pp.get("ys.user.info.real.count");
                if (countRel > Integer.parseInt(setCount)) {
                    SystemError.wrapBs(EnumType.ResponseCode.Fail.getCode(), "认证失败，每月最多认证" + setCount + "次");
                }
            }
            loginInfo.setRealName(realName);
            loginInfo.setRealIdCard(realIdCard);
            Map<String,Object> ys_mct = new HashMap<>();
            ys_mct.put("uid",uid);
            ys_mct.put("idCard",realIdCard);
            ys_mct.put("realName",realName);
            ys_mct.put("createTime", DateHandler.getLongFormatDate());
            int ii = 0;
            /**支付渠道  mb 摩宝 ys 银盛  sxy 首信易 um U盟 */
            String payChannel = StringHandler.getString(Config.pp.get("ys.user.pay.channel"), "ys");
            if (payChannel.equals("ys") && Boolean.parseBoolean(Config.pp.get("ys.user.real"))) {
                int userMerchant = userInfoService.getUserMerchantCount(uid);
                if (userMerchant > 0) {
                    SystemError.wrapBs("500", "该账户已申请钱包，请联系客服");
                }
                Map<String, Object> requestMap = new HashMap<>();
                String requestNo = BaseParam.getOrderSN();//业务请求号
                requestMap.put("requestNo", requestNo);
                requestMap.put("name", realName);    //姓名
                requestMap.put("certNo", realIdCard);    //证件号码
                requestMap.put("certType", "01");    //证件类型：01-身份证
                requestMap.put("mobile", phone);    // 手机号
                RequestData requestData = new RequestData(requestMap);
                YinShengClient yinShengClient = new YinShengClient(ServiceEnum.WALLET_REGISTER);
                Map<String, Object> map = yinShengClient.requestApi(requestData, "实名认证");
                if (StringHandler.isEmpty(map)) {
                    SystemError.wrapBs("500", "认证失败");
                }
                ys_mct.put("merchantNo", map.get("merchantNo"));
                ys_mct.put("requestNo", requestNo);
                ys_mct.put("createTime", DateHandler.getLongFormatDate());
            }
            DynamicDataSourceContextHolder.setDataSourceKey("ysIm");
            ii = transactionTemplate.execute(t -> {
                int i = 0;
                //添加银盛商户信息
                if(payChannel.equals("ys") && Boolean.parseBoolean(Config.pp.get("ys.user.real"))){
                    i = userInfoService.ysMerchantInsert(ys_mct);
                    if(i<=0){
                        SystemError.wrapBs("500","认证失败");
                    }
                }
                i = userInfoService.updateRealByUser(loginInfo);
                return i;
            });
            DynamicDataSourceContextHolder.clearDataSourceKey();
            if (ii != 1) {
                SystemError.wrapBs("500", "操作失败");
            }
            if(StringHandler.isNotEmpty(phoneKey)){
                Redis.key.del(UserKey.uidByPhone(phoneKey));
            }
            Redis.key.del(UserKey.userInfoByUid(uid));
        }
        LoginInfo loginInfo1 = loginService.infoByUid(uid);
        return ResponseHandler.success(loginService.userInfo(loginInfo1,token));
    }
    @RequestMapping("real/mb")
    public String realMb(@RequestAttribute String uid, String realName, String realIdCard,
                       @RequestParam(defaultValue = "") String phone,String token) {
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            SystemError.wrapBs("500", "系统维护,交易系统已关闭");
        }
        LoginInfo loginInfo = loginService.infoByUid(uid);
        String phoneKey = "";
        if(!phone.equals(loginInfo.phone)){
            phoneKey = UserKey.uidByPhone(phone);
        }
        if (StringHandler.isEmpty(realIdCard)) {
            SystemError.wrapBs("500", "身份证号码不能为空");
        }
        if (!IdcardUtil.isValidCard(realIdCard)) {
            return ResponseHandler.error("500", "身份证号码格式错误");
        }
        int age = IdcardUtil.getAgeByIdCard(realIdCard);
        if (age < 18 || age > 65) {
            SystemError.wrapBs("500", "为防止银行卡盗刷，本平台仅支持18-65岁以内用户使用");
        }
        if(StringHandler.isEmpty(phone) && StringHandler.isEmpty(loginInfo.getPhone())){
            SystemError.wrapBs("500", "请填写电话号码");
        }
        if(StringHandler.isNotEmpty(phone)){
            if(!phone.equals(loginInfo.getPhone())){
                loginInfo.setPhone(phone);
            }
        }else{
            phone = loginInfo.getPhone();
        }
        Integer id = userInfoService.getOpenAccountCountByPhone(phone);
        if (StringHandler.isNotEmpty(id)) {
            if(id!=StringHandler.getInt(uid)){
                SystemError.wrapBs("500", "该电话号码认证，不可重复操作");
            }
        }
        long id1 = userInfoService.getOpenAccountCountMbCardNo(realIdCard);

        if (id1!=0) {
            if(id1==StringHandler.getLong(uid)){
                Redis.key.del(UserKey.userInfoByUid(uid));
                return ResponseHandler.success();
            }
            SystemError.wrapBs("500", "该身份证已认证，不可重复操作");
        }
        if (realName.length() > StringHandler.getInt(Config.pp.getOrDefault("wallet.create.name.len", "15"))) {
            SystemError.wrapBs("500", "请输入身份证上的真实姓名");
        }
        String key = String.format("ys:user:info:real:count:%s:", uid);
        long countRel = Redis.key.increment(key);
        Redis.key.setKeyTimeout(key,30*24*60*60L);
        if (countRel>0) {
            String setCount = Config.pp.get("ys.user.info.real.count");
            if (countRel > Integer.parseInt(setCount)) {
                SystemError.wrapBs(EnumType.ResponseCode.Fail.getCode(), "认证失败，每月最多认证" + setCount + "次");
            }
        }
        loginInfo.setRealName(realName);
        loginInfo.setRealIdCard(realIdCard);
        Map<String,Object> ys_mct = new HashMap<>();
        ys_mct.put("uid",uid);
        ys_mct.put("idCard",realIdCard);
        ys_mct.put("realName",realName);
        ys_mct.put("createTime", DateHandler.getLongFormatDate());
        int count = userInfoService.getUserIdCordCount(uid);
        DynamicDataSourceContextHolder.setDataSourceKey("ysIm");
        String finalPhone = phone;
        int ii = transactionTemplate.execute(t -> {
            int i = 0;
            if(count>0){
                i = userInfoService.userIdCordUpdate(ys_mct);
            }else{
                i = userInfoService.userIdCordInsert(ys_mct);
            }
            if(i<=0){
                SystemError.wrapBs("500", "操作失败");
            }
            if(!finalPhone.equals(loginInfo.getPhone())) {
                i = userInfoService.updatePhone(uid, finalPhone);
                if (i <= 0) {
                    SystemError.wrapBs("500", "操作失败");
                }
            }
            return i;
        });
        DynamicDataSourceContextHolder.clearDataSourceKey();
        if (ii != 1) {
            SystemError.wrapBs("500", "操作失败");
        }
        if(StringHandler.isNotEmpty(phoneKey)){
            Redis.key.del(UserKey.uidByPhone(phoneKey));
        }
        Redis.key.del(UserKey.userInfoByUid(uid));
        LoginInfo loginInfo1 = loginService.infoByUid(uid);
        return ResponseHandler.success(loginService.userInfo(loginInfo1,token));
    }

    @PostMapping("/validatePayPwd")
    public String validatePayPwd(@RequestAttribute String uid, String payPass) {
        if (StringHandler.isEmpty(payPass)) {
            return ResponseHandler.error("500", "请输入支付密码");
        }
        LoginInfo loginInfo = loginService.infoByUid(uid);
        return ResponseHandler.success(loginInfo.getPayPass().equals(PasswordHelper.encryptPassword(RSAUtils.decryptField(payPass), loginInfo.getSalt())));
    }


    @PostMapping("updateIdcard")
    public String updateIdcard(@RequestAttribute String uid, String realName, String idCard,
                               @RequestParam(defaultValue = "") String phone,String token) {
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            SystemError.wrapBs("500", "交易系统已关闭");
        }
        if (StringHandler.isEmpty(realName)) {
            return ResponseHandler.error("500", "真实姓名必填");
        }
        if (StringHandler.isEmpty(idCard)) {
            return ResponseHandler.error("500", "身份证号码必填");
        }
        int age = IdcardUtil.getAgeByIdCard(idCard);
        if (age < 18 || age > 65) {
            SystemError.wrapBs("500", "为防止银行卡盗刷，本平台仅支持18-65岁以内用户使用");
        }
        if(StringHandler.isEmpty(phone)){
            SystemError.wrapBs("500", "请填写电话号码");
        }

        Integer id = userInfoService.getOpenAccountCountByPhone(phone);
        if (StringHandler.isNotEmpty(id)) {
            if(id!=StringHandler.getInt(uid)){
                SystemError.wrapBs("500", "该电话号码认证，不可重复操作");
            }
        }
        RealEntity realEntity = loginService.getOpenAccountCountByCardNo(idCard);
        //如果通过身份证查询数据  id不匹配  证明有人用过
        if (realEntity != null && !uid.equals(realEntity.getUid())) {
            SystemError.wrapBs("500", "该身份证已认证，不可重复操作");
        }
        LoginInfo loginInfo = loginService.infoByUid(uid);
        String phoneKey = "";
        if(!phone.equals(loginInfo.phone)){
            phoneKey = UserKey.uidByPhone(loginInfo.phone);
        }
        int count = loginDao.getUserRealInfoUpdateEntity(uid);
        if (count == StringHandler.getInt(Config.pp.getOrDefault(BaseConstants.DataSourceConstants.USER_UPDATE_REAL_COUNT,"3"))) {
            return ResponseHandler.error("500", "修改失败，每个用户实名只允许修改" +count+ "次！");
        }
        Map<String, Object> infoMap = loginService.getUserByUserId(uid);
        /**支付渠道  mb 摩宝 ys 银盛  sxy 首信易 um U盟 */
        String payChannel=StringHandler.getString(Config.pp.get("ys.user.pay.channel"),"ys");
        Map<String,Object> ys_mct = new HashMap<>();
        ys_mct.put("uid",uid);
        ys_mct.put("realIdCard",idCard);
        ys_mct.put("realName",realName);
        ys_mct.put("createTime", DateHandler.getLongFormatDate());
        int userMerchant = 0 ;
        if(payChannel.equals("ys") && Boolean.parseBoolean(Config.pp.get("ys.user.real"))){
            userMerchant = userInfoService.getUserMerchantCount(uid);
            if(userMerchant>0){
                SystemError.wrapBs("500","该账户已申请钱包，请联系客服");
            }
            Map<String,Object> requestMap = new HashMap<>();
            String requestNo = BaseParam.getOrderSN();//业务请求号
            requestMap.put("requestNo",requestNo);
            requestMap.put("name",realName);	//姓名
            requestMap.put("certNo",idCard);	//证件号码
            requestMap.put("certType","01");	//证件类型：01-身份证
            requestMap.put("mobile",phone);	// 手机号
            RequestData requestData = new RequestData(requestMap);
            YinShengClient yinShengClient = new YinShengClient(ServiceEnum.WALLET_REGISTER);
            Map<String, Object> map = yinShengClient.requestApi(requestData,"实名认证");
            if(StringHandler.isEmpty(map)){
                SystemError.wrapBs("500","认证失败");
            }
            ys_mct.put("merchantNo",map.get("merchantNo"));
            ys_mct.put("requestNo",requestNo);
        }
        int finalUserMerchant = userMerchant;
        DynamicDataSourceContextHolder.setDataSourceKey("ysIm");
        int ii = transactionTemplate.execute(t -> {
            int i = 0;
            //添加银盛商户信息
            if(payChannel.equals("ys") && Boolean.parseBoolean(Config.pp.get("ys.user.real"))){
                if(finalUserMerchant >0){
                    i = userInfoService.ysMerchantUpdate(ys_mct);
                }else{
                    i = userInfoService.ysMerchantInsert(ys_mct);
                }

                if(i<=0){
                    SystemError.wrapBs("500","修改失败");
                }
            }
            if (StringHandler.isNotEmpty(infoMap)) {
                if(StringHandler.isNotEmpty(infoMap.get("realName"))) {
                    loginService.insertEntity(uid, infoMap.get("realName"), infoMap.get("card"), infoMap.get("phone"));
                }
            }
            i = loginService.updateIdcard(uid, realName, idCard,phone);
            return i;
        });
        DynamicDataSourceContextHolder.clearDataSourceKey();
        if (ii <= 0) {
            SystemError.wrapBs("500", "操作失败");
        }
        if(StringHandler.isNotEmpty(phoneKey))
            Redis.key.del(phoneKey);
        Redis.key.del(UserKey.userInfoByUid(uid));
        loginInfo = loginService.infoByUid(uid);
        return ResponseHandler.success(loginService.userInfo(loginInfo,token));
    }

    @PostMapping("updateIdcard/mb")
    public String updateIdcardMb(@RequestAttribute String uid, String realName, String idCard,
                               @RequestParam(defaultValue = "") String phone,String token) {
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            SystemError.wrapBs("500", "交易系统已关闭");
        }
        if (StringHandler.isEmpty(realName)) {
            return ResponseHandler.error("500", "真实姓名必填");
        }
        if (StringHandler.isEmpty(idCard)) {
            return ResponseHandler.error("500", "身份证号码必填");
        }
        int age = IdcardUtil.getAgeByIdCard(idCard);
        if (age < 18 || age > 65) {
            SystemError.wrapBs("500", "为防止银行卡盗刷，本平台仅支持18-65岁以内用户使用");
        }
        if(StringHandler.isEmpty(phone)){
            SystemError.wrapBs("500", "请填写电话号码");
        }

        Integer id = userInfoService.getOpenAccountCountByPhone(phone);
        if (StringHandler.isNotEmpty(id)) {
            if(id!=StringHandler.getInt(uid)){
                SystemError.wrapBs("500", "该电话号码认证，不可重复操作");
            }
        }
        RealEntity realEntity = loginService.getOpenAccountCountByCardNo(idCard);
        //如果通过身份证查询数据  id不匹配  证明有人用过
        if (realEntity != null && !uid.equals(realEntity.getUid())) {
            SystemError.wrapBs("500", "该身份证已认证，不可重复操作");
        }
        int count = loginDao.getUserRealInfoUpdateEntity(uid);
        if (count == StringHandler.getInt(Config.pp.getOrDefault(BaseConstants.DataSourceConstants.USER_UPDATE_REAL_COUNT,"3"))) {
            return ResponseHandler.error("500", "修改失败，每个用户实名只允许修改" +count+ "次！");
        }
        Map<String, Object> infoMap = loginService.getUserByUserId(uid);
        Map<String,Object> ys_mct = new HashMap<>();
        ys_mct.put("uid",uid);
        ys_mct.put("realIdCard",idCard);
        ys_mct.put("realName",realName);
        ys_mct.put("createTime", DateHandler.getLongFormatDate());
        DynamicDataSourceContextHolder.setDataSourceKey("ysIm");
        int ii = transactionTemplate.execute(t -> {
            int i = 0;
            if (StringHandler.isNotEmpty(infoMap)) {
                if(StringHandler.isNotEmpty(infoMap.get("realName"))) {
                    loginService.insertEntity(uid, infoMap.get("realName"), infoMap.get("card"), infoMap.get("phone"));
                }
            }
            i = userInfoService.userIdCordUpdate(ys_mct);
            if(i<=0){
                SystemError.wrapBs("500", "操作失败");
            }
            i = userInfoService.updatePhone(uid,phone);
            if(i<=0){
                SystemError.wrapBs("500", "操作失败");
            }
            return i;
        });
        DynamicDataSourceContextHolder.clearDataSourceKey();
        if (ii <= 0) {
            SystemError.wrapBs("500", "操作失败");
        }
        Redis.key.del(UserKey.userInfoByUid(uid));
        LoginInfo loginInfo = loginService.infoByUid(uid);
        Redis.key.del(UserKey.uidByPhone(loginInfo.phone));
        return ResponseHandler.success(loginService.userInfo(loginInfo,token));
    }

    @RequestMapping("idCard/img/upload")
    public String idCardImgUpload(@RequestAttribute String uid,String zmImg,String zmImgType, String fmImg,String fmImgType,String token) {
        if (Boolean.parseBoolean(Config.pp.get("trading.disable"))) {
            SystemError.wrapBs("500", "系统维护,交易系统已关闭");
        }
        if(Logger.isOnline()){
            if (Boolean.parseBoolean(Config.pp.get("ys.idCard.img.upload"))) {
                SystemError.wrapBs("500", "该功能再开发中...");
            }
        }
        if(StringHandler.isEmpty(zmImg)){
            SystemError.wrapBs("500", "身份证正面不能为空");
        }
        if(StringHandler.isEmpty(fmImg)){
            SystemError.wrapBs("500", "身份证反面不能为空");
        }
        int userBank = userInfoService.getUserBankCount(uid);
        if(userBank<2){
            SystemError.wrapBs("500", " 当前用户未绑定2张以上银卡");
        }
        UserMerchant userMerchant = yinShengService.userMerchant(uid);
        if(StringHandler.isEmpty(userMerchant))
            SystemError.wrapBs("500","该账户还未实名认证");
//        if(!zmImg.contains(".jpg") && !zmImg.contains(".png"))
//            SystemError.wrapBs("500", "身份证正面上传失败,证件类型必须为jpg、png");
//        if(!fmImg.contains(".jpg") && !fmImg.contains(".png"))
//            SystemError.wrapBs("500", "身份证反面上传失败,证件类型必须为jpg、png");
        if(!zmImgType.equals("jpg") && !zmImgType.equals("png"))
            SystemError.wrapBs("500", "身份证正面上传失败,证件类型必须为jpg、png");
        if(!fmImgType.equals("jpg") && !fmImgType.equals("png"))
            SystemError.wrapBs("500", "身份证反面上传失败,证件类型必须为jpg、png");

//        String fileType = "";
//        if(zmImg.contains(".jpg")){
//            fileType = "jpg";
//        }else if(zmImg.equals(".png")){
//            fileType = "png";
//        }
//        String fileTypeFm = "";
//        if(fmImg.contains(".jpg")){
//            fileTypeFm = "jpg";
//        }else if(fmImg.equals(".png")){
//            fileTypeFm = "png";
//        }
        Map<String, Object> requestMap = new HashMap<>();
        String requestNo = BaseParam.getOrderSN();//业务请求号
        RequestData requestData = new RequestData();
        String zmCode = "";
        String fmCode = "";
        try {
            requestMap.put("requestNo", requestNo);
            requestMap.put("fileType", zmImgType);    //文件类型
            requestMap.put("fileBase64", zmImg);
            requestData.requestId = BaseParam.getOrderSN();
            YinShengClient yinShengClient = new YinShengClient(ServiceEnum.PIC_UPLOAD);
            //身份证正面
            Map<String, Object> map = yinShengClient.requestApi(requestData, requestMap, "统一文件上传");
            if (StringHandler.isEmpty(map)) {
                SystemError.wrapBs("500", "身份证正面上传失败");
            }
            zmCode = StringHandler.getString(map.get("fileCode"));
            //身份证反面
            requestMap = new HashMap<>();
            requestNo = BaseParam.getOrderSN();//业务请求号
            requestMap.put("requestNo", requestNo);
            requestMap.put("fileType", fmImgType);    //文件类型
            requestMap.put("fileBase64", fmImg);
            requestData = new RequestData();
            requestData.requestId = BaseParam.getOrderSN();
            Map<String, Object> mapFm = yinShengClient.requestApi(requestData, requestMap, "统一文件上传");
            if (StringHandler.isEmpty(mapFm)) {
                SystemError.wrapBs("500", "身份证反面上传失败");
            }
            fmCode = StringHandler.getString(mapFm.get("fileCode"));
        }catch (Exception e){
            log.error("统一文件上传错误",e);
        }
        //补充证件信息
        requestMap = new HashMap<>();
        requestNo = BaseParam.getOrderSN();//业务请求号
        requestMap.put("requestNo",requestNo);
        requestMap.put("merchantNo",userMerchant.merchantNo);
        requestMap.put("frontPicCode",zmCode);
        requestMap.put("backPicCode",fmCode);
        requestData = new RequestData(requestMap);
        YinShengClient yinShengClient1 = new YinShengClient(ServiceEnum.WALLET_SUPPLY_CARD_PIC);
        Map<String, Object> obj = yinShengClient1.requestApi(requestData,"钱包身份证照片补充");
        if(StringHandler.isEmpty(obj)){
            SystemError.wrapBs("500","身份证照片上传失败");
        }
        log.info("身份证照片上传---------"+JsonUtil.toJson(obj));
        //修改上传证件状态
        userInfoService.realIdCardUp(uid,2);
        Redis.key.del(UserKey.userInfoByUid(uid));
        LoginInfo loginInfo = loginService.infoByUid(uid);
        Redis.key.del(UserKey.uidByPhone(loginInfo.phone));
        return ResponseHandler.success(loginService.userInfo(loginInfo,token));
    }

    @RequestMapping("info")
    public String info(@RequestAttribute String uid,String token){
        LoginInfo loginInfo = loginService.infoByUid(uid);
        return ResponseHandler.success(loginService.userInfo(loginInfo,token));
    }
}
